import {fetch} from '@/utils/fetch'
import {progress, page, user, document, group, task, groupMember} from '../fragment'

export const saveBasicInfo = async ({nickName, avatarUrl}) => {
  let res = await fetch({
    query: `mutation {
      updateUserProfile(nickName: "${nickName}", avatarUrl: "${avatarUrl}") {
        access_token
        expires_in
        user_id
        avatar
        nick_name
      }
    }`
  })
  console.log(res.data.updateUserProfile)
  return res.data.updateUserProfile
}

export const getProgressesByPage = async ({pageIndex = 0, pageSize = 20, cache = true}) => {
  let res = await fetch({
    query: `{
      userProgresses (pageIndex: ${pageIndex}, pageSize: ${pageSize}) {
        ${page}
        rows {
          ${progress}
          task {
            ${task}
            group {
              ${group}
            }
          }
          document {
            ${document}
            user {
              ${user}
            }
          }
        }
      }
    }`,
    cache
  })
  return res.data.userProgresses
}

export const getGroups = async () => {
  let res = await fetch({
    query: `{
      groups (pageIndex: 0, pageSize: 200) {
        rows {
          ${group}
          users {
            ${user}
          }
        }
      }
    }`,
    cache: false
  })
  return res.data.groups.rows
}

export const getGroupById = async ({groupId, cache = true}) => {
  let res = await fetch({
    query: `{
      group (id: ${groupId}) {
        ${group}
        users {
          ${user}
        }
        members {
          ${groupMember}
        }
      }
    }`,
    cache
  })
  return res.data.group
}

export const getMyTotalProgressUnreadCount = async () => {
  let res = await fetch({
    query: `{
      progressGetMyUnreadCount
    }`,
  })
  return res.data.progressGetMyUnreadCount
}
